/**
 * Created by DELL on 2018/9/7.
 */
(function (ns) {



  // // 低音
  var audioSource2_1 = [
    {src: 'media/music/C.mp3'},
    {src: 'media/music/D.mp3'},
    {src: 'media/music/E.mp3'},
    {src: 'media/music/F.mp3'},
    {src: 'media/music/G.mp3'},
    {src: 'media/music/A.mp3'},
    {src: 'media/music/B.mp3'}
  ];
  // 中音
  var audioSource2_2 = [
    {src: 'media/music/C_1.mp3'},
    {src: 'media/music/D_1.mp3'},
    {src: 'media/music/E_1.mp3'},
    {src: 'media/music/F_1.mp3'},
    {src: 'media/music/G_1.mp3'},
    {src: 'media/music/A_1.mp3'},
    {src: 'media/music/B_1.mp3'}
  ];
  // 高音
  var audioSource2_3 = [
    {src: 'media/music/C_2.mp3'},
    {src: 'media/music/D_2.mp3'},
    {src: 'media/music/E_2.mp3'},
    {src: 'media/music/F_2.mp3'},
    {src: 'media/music/G_2.mp3'},
    {src: 'media/music/A_2.mp3'},
    {src: 'media/music/B_2.mp3'}
  ];
  // 超级高音
  var audioSource2_4 = [
    {src: 'media/music/C_2.mp3'},
    {src: 'media/music/D_2.mp3'},
    {src: 'media/music/E_2.mp3'},
    {src: 'media/music/F_2.mp3'},
    {src: 'media/music/G_2.mp3'},
    {src: 'media/music/A_2.mp3'},
    {src: 'media/music/B_2.mp3'}
  ];

  var Key = ns.Key = Hilo.Class.create({
    Extends: Hilo.Container,
    constructor: function (properties) {
      Key.superclass.constructor.call(this, properties);

      this.circle_img = properties.circle_img;
      this.key_index = properties.key_index;
      this.arc_img = properties.arc_img;
      this.width = properties.width;
      this.height = properties.height;
      this.music_type = properties.music_type;
      this.background = '#FED57D';
      this.x = properties.startX;
      this.y = -properties.height;
      this.stageHeight = properties.stageHeight;
      this.beatNum = properties.beatNum || 1;
      this.tone = properties.tone || 2;
      this.current_beatNum = properties.current_beatNum;  // 当前节拍
      this.speed = properties.stageHeight / ns.batTime;   // speed  屏幕高度 / (1/4)*1节拍对应时长
      this.moveTween = new Hilo.Tween(this, null, {
        onComplete: this.moveEnd.bind(this)
      });
      this.on(Hilo.event.POINTER_START, this.onUserclick.bind(this));
      this.on(Hilo.event.POINTER_END, this.onUserclickEnd.bind(this));
      this.initKey();
    },
    hasClick: false,  // 是否被点击过
    click_stageX: 0, // 点击事件的x，y坐标
    click_stageY: 0, // 点击事件的x，y坐标
    // 初始化按键内部
    initKey: function () {

      // 总高度
      var lastY = this.current_beatNum / 1000 * ns.keyBaseHeight * 2;
      this.y = -lastY - this.height;
      if (this.beatNum >= 1) {
        this.key_inner = new Hilo.View({
          id: 'key_inner',
          background: '#FECD64',
          top: 0,
          left: 0,
          width: this.width,
          height: this.height
        }).addTo(this);

        this.key_circle = new Hilo.Bitmap({
          id: 'key_circle',
          image: this.circle_img,
          x: (this.width - this.circle_img.width) / 2,
          y: this.height - 200
        }).addTo(this);

        this.key_arc = new Hilo.Bitmap({   // 弧形
          id: 'key_arc',
          image: this.arc_img,
          width: this.width,
          scaleX: this.width / this.arc_img.width,
          visible: false,
          x: 0,
          y: -this.arc_img.height
        }).addTo(this);


        this.key_line = new Hilo.View({
          id: 'key_line',
          background: '#EB3B3D',
          y: 0,
          x: this.width / 2 - 1,
          width: 2,
          height: this.height - 200
        }).addTo(this);

        /*长按高度动画*/
        this.touchTween = new Hilo.Tween(this.key_inner, null, {
          onComplete: this.touchEnd.bind(this)
        });
        /*中间的红线动画*/
        this.lineTween = new Hilo.Tween(this.key_line, null, {});
        /*弧形动画*/
        this.arcTween = new Hilo.Tween(this.key_arc, null, {});
        this.arcHeightTween = new Hilo.Tween(this.key_arc, null, {});
      }
      // this.startMove();
      // console.time("initAudio");
      this.initAudio();
      // console.timeEnd("initAudio");
    },
    initAudio: function () {
      if (this.music_type !== 0) { // 0 是静止音符
        var audio_src = '';

        var audioSource = audioSource2; // 中音
        if (this.tone === 1) { // 低音
          audioSource = audioSource1;
        } else if (this.tone === 3) { // 高音
          audioSource = audioSource3;
        } else if (this.tone === 4) {
          audioSource = audioSource4;
        }

        audio_src = audioSource[this.music_type - 1].src;

        this.audio = Hilo.WebSound.getAudio({
          src: audio_src,
          loop: false,
          volume: 1
        }).on('load', function(e){
          console.log('load');
        }).on('end', function(e){  // 音频播放完毕
          // console.log('end');
        });
        this.audio.load()
        // this.audio.preload = 'auto';
      }
    },
    playAudio: function () {
      if (this.music_type !== 0) { // 0 是静止音符
        this.audio.play();
      }
    },
    startMove: function () {
      // var height_batNum = this.batNum;
      // if (height_batNum < 1) {
      //   height_batNum = 1;
      // }
      var moveDuring = (this.stageHeight + this.height) / this.speed;  // 时长等于 总距离 / 速度
      //设置缓动时间
      this.moveTween.duration = moveDuring;
      //设置缓动的变换属性，即x从当前坐标变换到targetX
      this.moveTween.setProps({y: this.y}, {y: this.stageHeight});
      //启动缓动动画
      this.moveTween.start();
    },
    // 点击事件
    onUserclick: function (event) {
      if (game.state !== 'playing') { // 游戏中才能点击按键
        return false;
      }
      this.click_stageX = event.stageX;
      this.click_stageY = event.stageY;
      if (!this.hasClick) {
        this.hasClick = true;
        this.background = 'rgba(235,59,61,0.4)';

        // 音频播放
        if (this.music_type !== 0) { // 0是静音符
          this.playAudio()
        }
        // 更新分数
        ns.updataScore(this.key_index);
        // 保存按键记录
        ns.keepRecord({
          note: this.music_type,
          tone: this.tone
        });

        // 触摸移动事件 大于等于1拍的才会有内部阴影

        if (this.beatNum >= 1) {

          // 如果有圆圈 点击需要隐藏
          this.key_circle.visible = false;
          // 获取点击事件相对于按键的位置

          var bounds = this.getBounds();
          var startHeight = this.click_stageY - bounds.y - this.arc_img.height - 80;
          //设置缓动时间
          this.touchTween.duration = startHeight / this.speed;
          //设置缓动的变换属性，即x从当前坐标变换到targetX
          this.touchTween.setProps({height: startHeight}, {height: 0});
          //启动缓动动画
          this.touchTween.start();

          var inner_during = startHeight / this.speed;
          // 红线动画
          this.lineTween.duration = inner_during;

          this.lineTween.setProps({height: startHeight}, {height: 0});
          this.lineTween.start();

          // 弧形动画
          this.key_arc.visible = true;
          var arcTween_during = startHeight / this.speed;
          this.arcTween.duration = arcTween_during;
          this.arcTween.setProps({y: startHeight - 6}, {y: 0});
          this.arcTween.start();

          this.arcHeightTween.duration = this.arc_img.height / this.speed;
          this.arcHeightTween.delay = arcTween_during;
          this.arcHeightTween.setProps({height: this.arc_img.height}, {height: 0});
          this.arcHeightTween.start();
        }

      }
    },
    // 点击结束
    onUserclickEnd: function () {
      if (this.touchTween) {
        this.touchTween.stop();
      }
      if (this.arcTween) {
        this.arcTween.stop();
      }
      if (this.lineTween) {
        this.lineTween.stop();
      }
      if (this.arcHeightTween) {
        this.arcHeightTween.stop();
      }
    },
    moveEnd: function () {
      // console.log('移动完毕');
      this.removeFromParent();
    },
    touchEnd: function () {
      console.log('触摸完毕');
      // this.removeFromParent();
    },
    onUpdate: function () {
      if (this.hasClick) { // 已经点击了才进行碰撞检测
        // 检测由x和y参数指定的点是否在其外接矩形之内。
        var is_inRect = this.hitTestPoint(this.click_stageX, this.click_stageY);
        if (!is_inRect) { // 点击事件
          if (this.touchTween) {
            this.touchTween.stop();
          }
        }
      }
    }
  });

})(window.game);